﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.BLL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{

    /// <summary>
    /// 定位设备配置表
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GpsDeviceConfigController : ControllerBase
    {
        /// <summary>
        ///  增加定位配置信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Add(GpsDeviceConfigRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                Gpsdeviceconfig info = new Gpsdeviceconfig();
                info.ConfigId = obj.ConfigId;
                info.GpsDeviceId = obj.GpsDeviceId;
                info.BindId = obj.BindId;
                info.BindType = obj.BindType;
                info.CreateTime = obj.CreateTime ?? DateTime.Now;
                result.Status = GpsDeviceConfigBLL.Instance.Add(info);
                if (result.Status == -2)
                {
                    result.Status = -2;
                    result.Message = "CarNo Is Exist";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/GpsDeviceConfig/Add Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  编辑定位配置信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Update(GpsDeviceConfigRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                Gpsdeviceconfig info = new Gpsdeviceconfig();
                info.ConfigId = obj.ConfigId;
                info.BindId = obj.BindId;
                info.BindType = obj.BindType;
                info.CreateTime = obj.CreateTime??DateTime.Now;
                result.Status = GpsDeviceConfigBLL.Instance.Update(info);
                if (result.Status == -1)
                {
                    result.Message = "GpsDeviceConfig Is Not Exist";
                }
                if (result.Status == -2)
                {
                    result.Message = "GpsDevceNo Already Occupied";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/GpsDeviceConfig/Update Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  删除定位配置信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Remove(RemoveGpsDeviceConfigRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                result.Status = GpsDeviceConfigBLL.Instance.Remove(obj.ConfigId);
                if (result.Status == -1)
                {
                    result.Message = "ConfigId Is Not Exist";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/GpsDeviceConfig/Remove Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }


        /// <summary>
        ///  获取定位配置信息 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic, dynamic> GetByPage(GpsDeviceConfigShearchRequest obj)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            try
            {
                var page = Pickout.SetPage(obj);
                result.FirstParam = GpsDeviceConfigBLL.Instance.GetByPage(obj.BindType, page);
                result.SecondParam = page.TotalRows;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/GpsDeviceConfig/GetByPage Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  获取定位配置信息 单一 用户编号或者车辆编号
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> Get(GpsDeviceConfigShearchRequest2 obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                result.FirstParam = GpsDeviceConfigBLL.Instance.Get(obj.BindType,obj.BindId);
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/GpsDeviceConfig/Get Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

    }
}